/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package resource;

import com.myprofile.profile.MuseumItem;
import com.myprofile.profile.ObjectFactory;
import com.myprofile.profile.ProfileType;
import com.myprofile.profile.ProfileType.VisitedItems;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author lallarN
 */
public class CreateTours {
    
    public static void main(String[] args){
        ArrayList<Tour> tours = new ArrayList<Tour>();

        Inventory inv = new Inventory();
        List<MuseumItem> invItems = inv.getItems();

        ObjectFactory factory = new ObjectFactory();
        ProfileType t1 = factory.createProfileType();
        ProfileType t2 = factory.createProfileType();
        ProfileType t3 = factory.createProfileType();
        
        String[] interests = {"geometry", "tripods", "geography", "compasses", "batteries"};
        //String[] interests = {"astronomy", "art", "electrical engineering", "compasses"};
        
        /**
         * 
         * 
         *  FIND BEST TOUR TOUR THREE!!!!!!!!
         * 
         * 
         */
        List<MuseumItem> interestingItems = new ArrayList<MuseumItem>(15);

        int foundItems = 0;
        int currentIndex = 0;
        for (int i = interests.length; i >= 0; i--){
        	currentIndex = 0;
	        while (foundItems < 15 && currentIndex < invItems.size()){
	            MuseumItem item = invItems.get(currentIndex);
	            currentIndex++;
	            int interestingRate = 0;
	            for (int j = 0; j < interests.length; j++){
	                    if (item.getMaterial().contains(interests[j]) ||
	                    		item.getSubject().contains(interests[j]) ||
	                    		item.getObjectType().contains(interests[j])) {
	                        interestingRate++;
	                    }		
	            }
	            
	            if (interestingRate == i && !interestingItems.contains(item)){
	            	System.out.println(i);
	                interestingItems.add(item);
	                foundItems++;
	            }
	        }
        }
        
        VisitedItems visitedItems = factory.createProfileTypeVisitedItems();
        List<MuseumItem> t3items = visitedItems.getVisitedItem();	
        for (int i = 0; i < 15; i++){
                t3items.add(interestingItems.get(i));
        }
        t3.setVisitedItems(visitedItems);
        
        
        
        /****
         * 
         * 
         *  FIND SECOND BEST TOUR TOUR TWO!!!!
         * 
         * 
         */
        
        interestingItems = new ArrayList<MuseumItem>(15);

        foundItems = 0;
        currentIndex = 0;
        while (foundItems < 15){
                MuseumItem item = invItems.get(currentIndex);
                currentIndex++;
                for (int i = 0; i < interests.length; i++){
                	if (item.getMaterial().contains(interests[i]) ||
                    		item.getSubject().contains(interests[i]) ||
                    		item.getObjectType().contains(interests[i])) {
                			
                		if (!t3items.contains(item)){
                            interestingItems.add(item);
                            foundItems++;
                            break;
                		}
                    }		
                }
        }

        visitedItems = factory.createProfileTypeVisitedItems();
        List<MuseumItem> t2items = visitedItems.getVisitedItem();
        for (int i = 0; i < 15; i++){
                t2items.add(interestingItems.get(i));
        }

        t2.setVisitedItems(visitedItems);


        /******
         *  TOUR ONE !!!!!
         */
        interestingItems = new ArrayList<MuseumItem>();
        int inventorySize = invItems.size();
        for (int i = 0; i < 15; i++){
                int index = (int)(Math.random()*inventorySize);
                if (interestingItems.contains(invItems.get(index)) 
                		|| t3items.contains(invItems.get(index)) 
                		|| t2items.contains(invItems.get(index))) {
                	i--;
                    continue;
                } else {
                	interestingItems.add(invItems.get(index));
                }
        }
       
        visitedItems = factory.createProfileTypeVisitedItems();
        List<MuseumItem> t1items = visitedItems.getVisitedItem();
        for (MuseumItem item : interestingItems){
        	t1items.add(item);
        }
        t1.setVisitedItems(visitedItems);



        try {
                //dump profile into an XML file
                JAXBContext jc = JAXBContext.newInstance("com.myprofile.profile");
                Marshaller marshaller = jc.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                marshaller.marshal(t1 , new FileOutputStream("data/2-tour1.xml"));
                marshaller.marshal(t2, new FileOutputStream("data/2-tour2.xml"));
                marshaller.marshal(t3, new FileOutputStream("data/2-tour3.xml"));
        } catch (IOException ioe){
                ioe.printStackTrace();
        } catch (JAXBException e) {
                e.printStackTrace();
        }      
    }
}
